package org.example.Proxy;/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2024/12/25 14:44
 **/

import lombok.extern.slf4j.Slf4j;
import org.example.RpcApp;
import org.example.enums.ProtocolTypeEnum;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

//import static java.lang.reflect.Proxy.getInvocationHandler;

/**
 * @description TODO
 * @author sats@jz
 * @date 2024年12月25日 14:44
 */
@Slf4j
public class ServiceProxyFactory {

    private static final InvocationHandler invocationHandler = getInvocationHandler();
    public static <T> T getProxy(Class<T> tclass){
        return (T) Proxy.newProxyInstance(
                tclass.getClassLoader(),
                new Class[]{tclass},
               invocationHandler
        );
    }


    public static InvocationHandler getInvocationHandler() {
        if(RpcApp.getRpcConfig().isMock()) return new MockServiceProxy();
        String protocol = RpcApp.getRpcConfig().getProtocol();
        if(ProtocolTypeEnum.TCP.getValue().equals(protocol)){
            log.info("TCP 协议");
            return new ServiceProxyWithTcp();
        }
        if(ProtocolTypeEnum.HTTP.getValue().equals(protocol)){
            log.info("HTTP 协议");
            return new ServiceProxyWithRegistry();
        }
        return null;
    }


}
